var fs = require('fs')
var https = require('https')
var http = require('http')

//需要查询的问题id
var questionId = '41192909';
//请求地址
var url = '/api/v4/questions/' + questionId + '/answers?include=data%5B*%5D.is_normal%2Cis_sticky%2Ccollapsed_by%2Csuggest_edit%2Ccomment_count%2Ccan_comment%2Ccontent%2Ceditable_content%2Cvoteup_count%2Creshipment_settings%2Ccomment_permission%2Cmark_infos%2Ccreated_time%2Cupdated_time%2Crelationship.is_authorized%2Cis_author%2Cvoting%2Cis_thanked%2Cis_nothelp%2Cupvoted_followees%3Bdata%5B*%5D.author.badge%5B%3F(type%3Dbest_answerer)%5D.topics&offset=0&limit=20&sort_by=default';
//请求参数
var options = {
    hostname: 'www.zhihu.com',
    path: url,
    headers: {
        'Connection': 'keep - alive',
        'accept': 'application/json, text/plain, */*',
        'x-udid': 'AFBCiIC4jAuPTg_H2T6xod1-zUY40EzsoJs=',
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.81 Safari/537.36',
        'authorization': 'Bearer Mi4wQUZDQWxxVU9zd2tBVUVLSWdMaU1DeGNBQUFCaEFsVk5nSDBKV1FEbEpCQXExUGpPcldlR0c3Wm1DRHFjdUhQUXhB|1493390018|84c579c16349fa5c1564d9246a3bebe54b0f5ed5',
        'Referer': 'https://www.zhihu.com/question/31347237/answer/161296134',
        'Accept-Language': 'zh-CN,zh;q=0.8',
    },
    Cookie: 'q_c1=97997d7762594574b3fd464770596547|1491202167000|1491202167000; r_cap_id="OGEyMDkyYzAyZTZlNDhhY2I4YzEyNjA3YWIxNjI2ZjA=|1491202167|8ba0871bee83e66040fb885a348e9a51542f3704"; cap_id="Zjk2MGJkZDdmNGI4NGFhZTg0ZjliMTE5ZTk3MjcyYWU=|1491202167|522d0c48da68f2d081d35e665b3009b70b6eeaed"; l_cap_id="ZjcwNzc2NGE0MzA5NDNlZWJmYTlhMmY0ZTRhZTBmMzM=|1491202167|9e0cb68f377810207e7e6832096a3f7e0f19ed61"; d_c0="AFBCiIC4jAuPTg_H2T6xod1-zUY40EzsoJs=|1491202168"; _zap=d9103303-2526-47df-a1f2-6cbd2c1a58fe; aliyungf_tc=AQAAALLOqB4j5woAMWMNtxjN/A1mQQ9M; acw_tc=AQAAAKbglCNwaAsAMWMNt+s1L/AVyRTA; _xsrf=7e54552065134cbec99838183b897476; __utma=51854390.913840428.1491202169.1491202169.1493389595.2; __utmb=51854390.4.8.1493389654880; __utmc=51854390; __utmz=51854390.1493389595.2.2.utmcsr=baidu.com|utmccn=(referral)|utmcmd=referral|utmcct=/; __utmv=51854390.100-1|2=registration_date=20160331=1^3=entry_date=20160331=1; z_c0=Mi4wQUZDQWxxVU9zd2tBVUVLSWdMaU1DeGNBQUFCaEFsVk5nSDBKV1FEbEpCQXExUGpPcldlR0c3Wm1DRHFjdUhQUXhB|1493390018|84c579c16349fa5c1564d9246a3bebe54b0f5ed5',
    method: 'GET'
}

//封装请求函数
function requestData(options, callback) {
    console.log('开始请求--->' + options.path);
    var req = https.request(options, (res) => {
        console.log('状态码：', res.statusCode);
        var result = '';
        res.on('data', (d) => {
            result += d;
        });
        res.on('end', () => {
            callback(result);
        });
    }).on('error', (e) => {
        console.error(e);
    });
    req.end();
}

//检测文件或者文件夹存在 nodeJS
function fsExistsSync(path) {
    try {
        fs.accessSync(path, fs.F_OK);
    } catch (e) {
        return false;
    }
    return true;
}

//移除非法字符
function removeOtherChar(s) {
    var pattern = new RegExp("[`~!@#$^&*()=|{}':;',\\[\\].<>/?~！@#￥……&*（）——|{}【】‘；：”“'。，、？]")
    var rs = "";
    for (var i = 0; i < s.length; i++) {
        rs = rs + s.substr(i, 1).replace(pattern, '');
    }
    return rs;
}

/**
 * 下载图片
 * @param {String} url 
 * @param {String} filePath 
 */
function downloadFile(url, folderPath, fileName) {
    console.log('下载图片==>' + url);
    https.get(url, res => {
        var result = '';
        //一定要设置这个，不然图片打不开！
        res.setEncoding('binary');
        res.on('data', (d) => {
            result += d;
        });
        res.on('end', () => {
            if (!fsExistsSync(folderPath)) {
                fs.mkdirSync(folderPath);
            }
            let path = folderPath + fileName;
            fs.writeFile(path, result, 'binary', err => {
                if (err) {
                    console.log('图片：' + url + '保存失败！！！');
                    console.log(err);
                } else {
                    console.log('下载图片' + path + '---DONE!');
                }
            });
        });
    }).on('error', (e) => {
        console.error(e);
    });
}

var imgFolderPath = 'D://images';
var fileNameNum = 1;
//解析文本中的图片并下载
function parseContentImg(answerData) {
    let content = answerData.content;
    let title = removeOtherChar(answerData.question.title);
    let result = content.match(/\bsrc=\"([^\"]*)_b.jpg\"/g);
    if (undefined != result) {
        for (var index = 0; index < result.length; index++) {
            var url = result[index];
            url = url.substring(5, url.length - 1);
            let folderPath = imgFolderPath + '//' + title + '//';
            removeOtherChar(folderPath);
            let fileName = fileNameNum + '.jpg';
            downloadFile(url, folderPath, fileName);
            fileNameNum += 1;
        }
    }
}

//发送请求
requestData(options, function parseData(result) {
    var resultData = '';
    try {
        resultData = JSON.parse(result)
    } catch (e) {
        console.log('解析JSON发生异常:' + e);
        requestData(options, parseData);
        return;
    }

    if (null != resultData && undefined != resultData && undefined != resultData.paging.next) {
        if (resultData.data.length > 0) {
            //解析数据
            for (var index = 0; index < resultData.data.length; index++) {
                var element = resultData.data[index];
                console.log('开始处理用户[' + element.author.url_token + ']的回答');
                parseContentImg(element);
            }
            //继续请求
            options.path = resultData.paging.next;
            requestData(options, parseData);
        } else {
            console.log('图片已经全部下载完成！！！');
            return;
        }
    }
});